<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

</head>
<body>
<form class="form-inline definewidth m20" action="" method="get">    
   <table>
		<tr>
			<td align="right">原密码:</td><td><input type="password" id="oldPassword" onblur="checkPassword()"/><span id="msg" ></span></td>
		</tr>
		<tr>
			<td align="right">新密码:</td><td><input type="password" id="p1"/><span class="p1"></span></td>
		</tr>
		<tr>
			<td align="right">确认密码:</td><td><input type="password" id="p2" /><span class="p2"></span></td>
		</tr>
		<tr>
			<td colspan="2" align="center"><br/>
				<input type="button" id="save" value="保存" class="btn btn-primary"/>
			</td>
		</tr>
   </table>
</form>
<script>

	String.prototype.GetValue= function(para) {
		let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
		let r = this.substr(this.indexOf("\?")+1).match(reg);
		if (r!=null) return unescape(r[2]); return null;
	}

	//获取登录账号的id
	let url=document.location.toString();
	var sysUserId = url.GetValue("sysUserId");

	//核实原密码是否正确
	function checkPassword() {
		$.ajax({
			type: "GET",
			url: "/password/"+sysUserId,
			data:"password="+$("#oldPassword").val(),
			dataType:"json",
			success: function(vo){
				console.log(vo);
				if(vo.code==200){
					$("#msg").html(vo.msg);
					$("#msg").css("color","green");
				}else {
					$("#msg").html(vo.msg);
					$("#msg").css("color","red");
				}
			}
		});
	}

	//更新密码
	$("#save").click(function () {
		if ($("#p1").val()!=$("#p2").val()){
			$(".p2").html("两次密码不同").css("color","red");
		}else {
			$(".p2").html("两次密码相同").css("color","green");

			$.ajax({
				type:"POST",
				url: "/password/update",
				data:"sysUserId="+sysUserId+"&newPassword="+$("#p1").val()+"&_method=PUT",
				dataType:"json",
				success: function(vo){
					alert(vo.msg);
				}
			});
		}
	})

</script>
</body>
</html>